package com.sj.purchase.modules.cost.config.task;

import com.sj.purchase.modules.cost.config.task.synchronizedata.SyncTaskManager;
import com.sj.purchase.modules.cost.domain.SucTask;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;

/**
 * 调度者 负责调度定时任务
 * Created by yangrd on 2017/8/5
 **/
@Configuration
public class Scheduler {

    @Autowired
    private SyncTaskManager syncTaskManager;

    private Date yesterday = null;

    @Scheduled(cron = "0 3 0 * * ?")
    public void now() {
        yesterday = DateUtils.addDays(new Date(),-1);
    }

    @Scheduled(cron = "0 5 0 * * ?")
    public void stockTask() {
        runTask(SucTask.TaskType.SYNC_STOCK);
    }

    @Scheduled(cron = "0 20 0 * * ?")
    public void orderTask() {
        runTask(SucTask.TaskType.SYNC_IN_ORDER);
        runTask(SucTask.TaskType.SYNC_OUT_ORDER);
    }

    @Scheduled(cron = "0 40 0 * * ?")
    public void adjustOrderTask() {
        runTask(SucTask.TaskType.SYNC_ADJUSTMENT);
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void inventoryTask() {
        runTask(SucTask.TaskType.SYNC_INVENTORY);
    }

    private void runTask(SucTask.TaskType taskType) {
        syncTaskManager.runTask(taskType, yesterday);
    }


}
